package com.ty.mianshiya.aop;

import com.ty.mianshiya.annotation.DistributedLock;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 切面类，用于处理分布式锁注解
 */
@Aspect
@Component
public class DistributedLockAspect {

    @Resource
    private RedissonClient redissonClient;

    public Object around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) throws Exception{

        String lockKey = distributedLock.key();
        long waitTime = distributedLock.waitTime();
        long leaseTime = distributedLock.leaseTime();
        TimeUnit timeUnit = distributedLock.timeUnit();

        //从redis中获取distributedLock
        RLock lock = redissonClient.getLock(lockKey);
        boolean acquired=false;
        try{
            //尝试获取锁
            acquired = lock.tryLock(waitTime, leaseTime, timeUnit);
            if(acquired){
                //获取锁成功，执行目标方法
                return joinPoint.proceed();
            }else{
                //获取锁失败，抛出异常或处理逻辑
                throw new RuntimeException("Could not acquire lock:"+lockKey);
            }
        }catch (Throwable e){
            throw new Exception();
        }finally {
            if(acquired){
                //释放锁
                lock.unlock();
            }
        }
    }
}
